﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SHomeWorkshop.LunarPresentation.Interfaces;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void ExpandAndCollapse()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if ((curEx is ICollapsed) == false)
            {
                MessageBox.Show("此类型公式不支持“展开/折叠”操作！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;// 未实现这个接口，不支持。
            }

            if (curEx is Expressions.BaseText)
            {
                if (curEx.ParentPanel != null && curEx.ParentPanel.Direction == Orientation.Horizontal)
                {
                    MessageBox.Show("水平子面板中的文本块不支持“展开/折叠”操作！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                Expressions.BaseText bt = curEx as Expressions.BaseText;

                if (bt.IsHeaderText == false)
                {
                    MessageBox.Show("当前文本块不是标题，不支持折叠！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                if (bt.Indent > 3)
                {
                    MessageBox.Show("当前文本块缩进级别过大，折叠操作无意义！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                List<Expressions.Expression> subExs = bt.GetSubIndentExpressions();
                if (subExs == null || subExs.Count <= 0)
                {
                    MessageBox.Show("当前文本块没有“下级”公式，折叠操作无意义！",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            Expressions.LeafPanel leaf = curEx as Expressions.LeafPanel;
            if (leaf != null && leaf.LeafType == Expressions.LeafPanel.LeafPanelType.Root)
            {
                MessageBox.Show("树的叶首面板不支持[折叠∕展开]操作！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            //这将打断拖动过程，防止出现错误。
            if (Globals.isDraging)
            {
                Globals.isDraging = false;
                Globals.dragSourceExpression = null;
            }

            ICollapsed ic = curEx as ICollapsed;

            ModifingItem mi = new ModifingItem();
            Action action = new Action(curEx.ExID, -1, curEx.GetType().Name,
                "IsCollapsed", ic.IsCollapsed.ToString(), (!ic.IsCollapsed).ToString());

            ic.IsCollapsed = !ic.IsCollapsed;

            mi.Add(action);
            curEx.Editor.Do(mi);
        }
    }
}
